﻿/*
-----------------------------------------------------------------------------
This source file is part of Cell Cloud.

Copyright (c) 2009-2012 Cell Cloud Team - cellcloudproject@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/

#ifndef CellPrerequisites_h
#define CellPrerequisites_h

// Platform-specific stuff
#include "CellPlatform.h"

// Needed for CELL_WCHAR_T_STRINGS below
#include <string>

// 配置内存追踪
#if CELL_DEBUG_MODE
#	if CELL_MEMORY_TRACKER_DEBUG_MODE
#		define CELL_MEMORY_TRACKER 1
#	else
#		define CELL_MEMORY_TRACKER 0
#	endif
#else
#	if CELL_MEMORY_TRACKER_RELEASE_MODE
#		define CELL_MEMORY_TRACKER 1
#	else
#		define CELL_MEMORY_TRACKER 0
#	endif
#endif

namespace cell
{

// 定义 Cell 的版本
#define CELL_VERSION_MAJOR 0
#define CELL_VERSION_MINOR 1
#define CELL_VERSION_REVISION 6

#if CELL_DEBUG_MODE
	#define CELL_VERSION_SUFFIX "debug"
#else
	#define CELL_VERSION_SUFFIX "release"
#endif
	#define CELL_VERSION_NAME "Origin"

#define CELL_VERSION    ((CELL_VERSION_MAJOR << 16) | (CELL_VERSION_MINOR << 8) | CELL_VERSION_REVISION)

// 定义 HashMap 和 HashSet
#if CELL_COMPILER == CELL_COMPILER_GNUC && CELL_COMP_VER >= 310 && !defined(STLPORT)
#   if CELL_COMP_VER >= 430
#       define HashMap ::std::tr1::unordered_map
#		define HashSet ::std::tr1::unordered_set
#    else
#       define HashMap ::__gnu_cxx::hash_map
#       define HashSet ::__gnu_cxx::hash_set
#    endif
#else
#   if CELL_COMPILER == CELL_COMPILER_MSVC
#       if CELL_COMP_VER >= 1600 // VC++ 10.0
#			define HashMap ::std::tr1::unordered_map
#           define HashSet ::std::tr1::unordered_set
#		elif CELL_COMP_VER > 1300 && !defined(_STLP_MSVC)
#           define HashMap ::stdext::hash_map
#           define HashSet ::stdext::hash_set
#       else
#           define HashMap ::std::hash_map
#           define HashSet ::std::hash_set
#       endif
#   else
#       define HashMap ::std::hash_map
#       define HashSet ::std::hash_set
#   endif
#endif

/** 简化原始数据类型定义
*/
typedef unsigned char uchar;
typedef unsigned short ushort;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef unsigned char byte;

// 定义线程库
#include "threading/CellThreadDefines.h"

// 前置声明，减少包含的头文件依赖
class ActionDelegate;
class ActionDialect;
class Adapter;
class BlockingAcceptor;
class BlockingConnector;
class ByteBuffer;
class CachedThreadPool;
class CloudComputingLocator;
class Cellet;
class CelletFeature;
class CelletSupervisor;
class CharBuffer;
class CloudGraph;
class CrashReport;
class Cryptology;
class ContactService;
class DatagramAcceptor;
class Date;
class Dialect;
class DialectEnumerator;
class DialectFactory;
class DynLib;
class DynLibManager;
class Endpoint;
class ExecutorListener;
class ExecutorManager;
class ExecutorService;
class FileExpress;
class FileExpressContext;
class FileExpressListener;
class FileExpressServoContext;
class FileExpressTask;
class FixedThreadPool;
class Future;
class Gene;
class Grade;
class HttpListener;
class HttpConnection;
class HttpRequest;
class HttpResponse;
class Inet4Address;
class Inet6Address;
class InetAddress;
class License;
class Logger;
class LoggerManager;
class Mapper;
class MemoryTracker;
class Message;
class MessageAcceptor;
class MessageConnector;
class MessageHandler;
class MessageService;
class NonblockingAcceptor;
class Nucleus;
class Observable;
class Observer;
class Packet;
class Performance;
class Primitive;
class Promoter;
class Properties;
class PropertyGene;
class Reducer;
class RelationalNucleusAdapter;
class RelationalNucleusAdapterContext;
class RelationalNucleusAdapterDriver;
class ResultSet;
class Runnable;
class ScheduledExecutorService;
class ScheduledFuture;
class ScheduledThreadPoolExecutor;
class Schema;
class Section;
class Serializable;
class Serializer;
class SerializerStream;
class Service;
class Session;
class SingleFileStorage;
class Speaker;
class SQLiteStorage;
class Storage;
class StorageDriver;
class StorageEnumerator;
class StorageFactory;
class StorageMaster;
class Stream;
class Stuff;
class TalkListener;
class TalkService;
class TalkWebService;
class Thread;
class ThreadPoolExecutor;
class WebAddon;
class WorkChain;

} // end namespace cell

// 包含标准库的头文件
#include "CellStdHeaders.h"
#include "CellMemoryAllocatorConfig.h"

namespace cell
{
// 定义 UUID
typedef boost::uuids::uuid UUID;
#define RANDOM_UUID boost::uuids::random_generator()()
#define UUID_TO_STRING(tag) boost::uuids::to_string(tag)

}

#define EXPORT_BOOST_NONCOPYABLE( exportmacro ) \
	class exportmacro boost::noncopyable_::noncopyable;

namespace cell
{
#if CELL_STRING_USE_CUSTOM_MEMORY_ALLOCATOR
#if CELL_WCHAR_T_STRINGS
	typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, STLAllocator<wchar_t,GeneralAllocPolicy > >	_StringBase;
#else
	typedef std::basic_string<char, std::char_traits<char>, STLAllocator<char,GeneralAllocPolicy > >	_StringBase;
#endif

#if CELL_WCHAR_T_STRINGS
	typedef std::basic_stringstream<wchar_t,std::char_traits<wchar_t>,STLAllocator<wchar_t,GeneralAllocPolicy > > _StringStreamBase;
#else
	typedef std::basic_stringstream<char,std::char_traits<char>,STLAllocator<char,GeneralAllocPolicy > > _StringStreamBase;
#endif

#define StdStringT(T) std::basic_string<T, std::char_traits<T>, std::allocator<T> >	
#define CustomMemoryStringT(T) std::basic_string<T, std::char_traits<T>, STLAllocator<T,GeneralAllocPolicy> >	

template<typename T>
bool operator <(const CustomMemoryStringT(T)& l,const StdStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())<0;
}
template<typename T>
bool operator <(const StdStringT(T)& l,const CustomMemoryStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())<0;
}
template<typename T>
bool operator <=(const CustomMemoryStringT(T)& l,const StdStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())<=0;
}
template<typename T>
bool operator <=(const StdStringT(T)& l,const CustomMemoryStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())<=0;
}
template<typename T>
bool operator >(const CustomMemoryStringT(T)& l,const StdStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())>0;
}
template<typename T>
bool operator >(const StdStringT(T)& l,const CustomMemoryStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())>0;
}
template<typename T>
bool operator >=(const CustomMemoryStringT(T)& l,const StdStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())>=0;
}
template<typename T>
bool operator >=(const StdStringT(T)& l,const CustomMemoryStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())>=0;
}

template<typename T>
bool operator ==(const CustomMemoryStringT(T)& l,const StdStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())==0;
}
template<typename T>
bool operator ==(const StdStringT(T)& l,const CustomMemoryStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())==0;
}

template<typename T>
bool operator !=(const CustomMemoryStringT(T)& l,const StdStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())!=0;
}
template<typename T>
bool operator !=(const StdStringT(T)& l,const CustomMemoryStringT(T)& o)
{
	return l.compare(0,l.length(),o.c_str(),o.length())!=0;
}

template<typename T>
CustomMemoryStringT(T) operator +=(const CustomMemoryStringT(T)& l,const StdStringT(T)& o)
{
	return CustomMemoryStringT(T)(l)+=o.c_str();
}
template<typename T>
CustomMemoryStringT(T) operator +=(const StdStringT(T)& l,const CustomMemoryStringT(T)& o)
{
	return CustomMemoryStringT(T)(l.c_str())+=o.c_str();
}

template<typename T>
CustomMemoryStringT(T) operator +(const CustomMemoryStringT(T)& l,const StdStringT(T)& o)
{
	return CustomMemoryStringT(T)(l)+=o.c_str();
}

template<typename T>
CustomMemoryStringT(T) operator +(const StdStringT(T)& l,const CustomMemoryStringT(T)& o)
{
	return CustomMemoryStringT(T)(l.c_str())+=o.c_str();
}

template<typename T>
CustomMemoryStringT(T) operator +(const T* l,const CustomMemoryStringT(T)& o)
{
	return CustomMemoryStringT(T)(l)+=o;
}

#undef StdStringT
#undef CustomMemoryStringT

#else // CELL_STRING_USE_CUSTOM_MEMORY_ALLOCATOR

#if CELL_WCHAR_T_STRINGS
	typedef std::wstring _StringBase;
#else
	typedef std::string _StringBase;
#endif

#if CELL_WCHAR_T_STRINGS
	typedef std::basic_stringstream<wchar_t,std::char_traits<wchar_t>,std::allocator<wchar_t> > _StringStreamBase;
#else
	typedef std::basic_stringstream<char,std::char_traits<char>,std::allocator<char> > _StringStreamBase;
#endif

#endif

typedef _StringBase String;
typedef _StringStreamBase StringStream;
typedef StringStream stringstream;

} // end namespace cell

//for stl containter
namespace cell
{ 
template <typename T, typename A = STLAllocator<T, GeneralAllocPolicy> > 
struct deque 
{ 
#if CELL_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR
	typedef typename std::deque<T, A> type;    
#else
	typedef typename std::deque<T> type;    
#endif
};

template <typename T, typename A = STLAllocator<T, GeneralAllocPolicy> > 
struct vector 
{ 
#if CELL_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR
	typedef typename std::vector<T, A> type;    
#else
	typedef typename std::vector<T> type;    
#endif
};

template <typename T, typename A = STLAllocator<T, GeneralAllocPolicy> > 
struct list 
{ 
#if CELL_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR
	typedef typename std::list<T, A> type;    
#else
	typedef typename std::list<T> type;    
#endif
};

template <typename T, typename P = std::less<T>, typename A = STLAllocator<T, GeneralAllocPolicy> > 
struct set 
{ 
#if CELL_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR
	typedef typename std::set<T, P, A> type;    
#else
	typedef typename std::set<T, P> type;    
#endif
};

template <typename K, typename V, typename P = std::less<K>, typename A = STLAllocator<std::pair<const K, V>, GeneralAllocPolicy> > 
struct map 
{ 
#if CELL_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR
	typedef typename std::map<K, V, P, A> type; 
#else
	typedef typename std::map<K, V, P> type; 
#endif
};

template <typename K, typename V, typename P = std::less<K>, typename A = STLAllocator<std::pair<const K, V>, GeneralAllocPolicy> > 
struct multimap 
{ 
#if CELL_CONTAINERS_USE_CUSTOM_MEMORY_ALLOCATOR
	typedef typename std::multimap<K, V, P, A> type; 
#else
	typedef typename std::multimap<K, V, P> type; 
#endif
};

} // end namespace cell


namespace cell
{
// 获取地址
template<typename T>
String peek(T* pointer)
{
	char id[64] = {0x0};
	sprintf(id, "%p", pointer);
	return String(id);
}

template<typename T>
void peek(char* output, T* pointer)
{
	sprintf(output, "%p", pointer);
}

/** 变量字面意义。
*/
enum LiteralBase
{
	LB_STRING = 1,
	LB_INT,
	LB_UINT,
	LB_LONG,
	LB_ULONG,
	LB_BOOL,
	LB_STREAM,
	LB_UNKNOWN = 0
};

// StringMap
typedef HashMap<String, String> StringMap;

// Cellets
typedef HashMap<String, Cellet*> Cellets;

} // end namespace cell

#endif // CellPrerequisites_h
